<template>
	<view>
		<text @click="payDemo(1)">支付宝支付</text></br>
		<text @click="payDemo(2)">微信支付</text>
	</view>
</template>

<script>
	import api from '@/common/lib/request.js';
	export default {
		data() {
			return {};
		},
		methods: {
			// type=1 支付宝支付, type=2 微信支付
			payDemo(type) {
				api.request({
						method: 'POST',
						coverUrl: 'http://www.creb.com.cn/api/front/content/appPay.jspx',
						data: {
							token: '8611d86745b444b0aec01e83278af479',
							contentId: 96248,
							type: type
						}
					})
					.then(response => {
						console.log(response);
						if (type == 1) {
							this.aliPay(response);
						} else if (type = 2) {
							this.wechatPay(response);
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			aliPay(orderInfo) {
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: orderInfo,
					success: function(res) {
						uni.showToast({
							title: '支付成功',
							duration: 2000
						});
						console.log('success:' + JSON.stringify(res));
					},
					fail: function(err) {
						uni.showToast({
							title: '支付失败',
							duration: 2000
						})
						console.log('fail:' + JSON.stringify(err));
					}
				})
			},
			wechatPay(res) {
				// let orderInfo = {
				//           appid: res.appId,
				//           noncestr: res.nonceStr,
				//           package: 'Sign=WXPay',
				//           partnerid: '1536451631',
				//           prepayid: res.prepay_id,
				//           timestamp: res.timeStamp,
				//           sign: res.sign
				//         };
				let orderInfo = {
					appid: res.appId,
					noncestr: res.nonceStr,
					package: 'Sign=WXPay',
					partnerid: '1536451631',
					prepayid: res.prepay_id,
					timestamp: res.timeStamp,
					sign: res.sign
				};
				uni.requestPayment({
					provider: 'wxpay',
					orderInfo: JSON.stringify(orderInfo),
					success: function(res) {
						uni.showToast({
							title: '支付成功',
							duration: 2000
						});
						console.log('success:' + JSON.stringify(res));
					},
					fail: function(err) {
						uni.showToast({
							title: '支付失败',
							duration: 2000
						});
						console.log('fail:' + JSON.stringify(err));
					}
				});
			}
		}
	};
</script>

<style>
</style>
